package com;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class ApiObservable {

    final ExecutorService threadPool = Executors.newSingleThreadExecutor();

    /**
     * 开始打印
     *
     * @return
     */
    public Observable<Integer> startPrint() {
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(final ObservableEmitter<Integer> emitter) throws Exception {
//                threadPool.execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println("000");
////                        emitter.onError(new Throwable());
//                        emitter.onNext(0);
//                    }
//                });
                Future<Integer> future = threadPool.submit(new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return 333;
                    }
                });
                try {
                    int value = future.get(5, TimeUnit.SECONDS);
//                    emitter.onNext(value);
                    emitter.onComplete();
                } catch (InterruptedException | ExecutionException | TimeoutException e) {
                    future.cancel(true);
                    emitter.onError(e);
                }



            }
        });

        return observable;
    }

    /**
     * 停止打印
     *
     * @return
     */
    public Observable<Integer> stopPrint() {
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(final ObservableEmitter<Integer> emitter) throws Exception {
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("111");
                        emitter.onNext(1);
                        emitter.onComplete();
                    }
                });
            }
        });

        return observable;
    }
}
